﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Clipper.Model;

namespace Clipper.ClipperUserControl
{
    /// <summary>
    /// UserQC.xaml 的交互逻辑
    /// </summary>
    public partial class UserQC : UserControl
    {
        #region 变量
        BLL.DetectItems mDetectItemBll = new BLL.DetectItems();
        BLL.QCHistory mQCHistoryBll = new BLL.QCHistory();
        ObservableCollection<Model.QCHistory> mQCHistoryData = null;
        ObservableCollection<Point> mQC1 = null;//质控1
        ObservableCollection<Point> mQC2 = null;//质控2
        ObservableCollection<QCDataItem> mQCData1 = null;//质控数据1
        ObservableCollection<QCDataItem> mQCData2 = null;//质控数据2
        public UserQC()
        {
            InitializeComponent();
        }
        #endregion

        public void DataBind()
        {
            if (cboDetectItem.ItemsSource == null)
            {
                cboDetectItem.ItemsSource = mDetectItemBll.GetSelectItemsList();
            }
        }
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            string dateStart, dateEnd;
            string detectItem = "";

            if (cboDetectItem.SelectedValue != null)
            {
                detectItem = cboDetectItem.SelectedValue.ToString();
            }
            else
            {
                UIHelper.ShowKeyMessage("SelectItemTip");
                return;
            }
            if (dtpStart.SelectedDate == null)
            {
                UIHelper.ShowKeyMessage("StartDateTip");
                return;
            }
            if (dtpEnd.SelectedDate == null)
            {
                UIHelper.ShowKeyMessage("EndDateTip");
                return;
            }
            dateStart = dtpStart.SelectedDate.Value.ToString("yyyy-MM-dd");
            dateEnd = dtpEnd.SelectedDate.Value.ToString("yyyy-MM-dd");

            mQCHistoryData = mQCHistoryBll.GetQCHistory(detectItem, dateStart, dateEnd);
            if (mQCHistoryData != null && mQCHistoryData.Count > 0)
            {
                
                int count = 0;
                mQC1 = new ObservableCollection<Point>();
                mQC2 = new ObservableCollection<Point>();
                mQCData1 = new ObservableCollection<QCDataItem>();
                mQCData2 = new ObservableCollection<QCDataItem>();
                foreach (Model.QCHistory model in mQCHistoryData)
                {
                    count++;
                    mQC1.Add(new Point(count, model.chrome1));
                    mQC2.Add(new Point(count, model.chrome2));
                    mQCData1.Add(new QCDataItem(count,model.QCDatetime,model.chrome1));
                    mQCData2.Add(new QCDataItem(count,model.QCDatetime, model.chrome2));
                }
                //图
                QCChart1.ShowData(mQC1);
                QCChart2.ShowData(mQC2);
                //数据
                clnRange1.Header = UIHelper.GetResourceText("QCRange1");
                clnRange2.Header = UIHelper.GetResourceText("QCRange2");
                datQC1.ItemsSource = mQCData1;
                datQC1.ItemsSource = mQCData2;

                txtAverage1.Text = QCChart1.Average.ToString();
                txtCV1.Text = QCChart1.CV.ToString();
                txtSD1.Text = QCChart1.SD.ToString();

                txtAverage2.Text = QCChart2.Average.ToString();
                txtCV2.Text = QCChart2.CV.ToString();
                txtSD2.Text = QCChart2.SD.ToString();
            }
            
        }

        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            if (datQC1.SelectedItem != null)
            {
                Model.QCDataItem model = datQC1.SelectedItem as Model.QCDataItem;
            }

        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (datQC1.SelectedItem != null)
            {

            }
        }
    }
}
